import { Body, Controller, Get, Post } from '@nestjs/common';
import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto';
import { ResultData } from 'src/common/utils/result';
import { RequireRole } from 'src/common/decorator/require-role.decorator';
import { RoleEnum } from 'src/common/enum';
import { RequirePermission } from 'src/common/decorator/require-perm.decorator';
import { PermService } from '../perm/perm.service';
import { User, UserDto } from '../user/user.decorator';
import { In } from 'typeorm';

@Controller('menu')
export class MenuController {
  constructor(
    private readonly menuService: MenuService,
    private readonly permService: PermService,
  ) {}

  @Get('perm')
  // @RequirePermission('system:menu:list')
  async getPermMenu(@User() user: UserDto) {
    const pageIds = await this.permService.getUserPageIds(user);
    let menus = await this.menuService.find({
      where: { pageId: In(pageIds) },
      relations: ['page'],
    });
    menus = menus.map((menu) => ({
      ...menu,
      ...menu.page,
    }));
    const menutree = await this.menuService.menuTree(menus);
    return ResultData.ok(menutree);
  }

  @Post()
  @RequireRole([RoleEnum.sys_admin])
  async createMenu(@Body() createMenuDto: CreateMenuDto) {
    const result = await this.menuService.create(createMenuDto);
    return ResultData.ok(result);
  }
}
